package com.health.mapp.interImpl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import net.sf.json.JSONObject;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.health.commons.PropertiesService;
import com.health.db.model.ThirdYHDJPayRecord;
import com.health.db.model.UserLIST;
import com.health.db.service.user.IUserLISTService;
import com.health.db.service.sm.IThirdYHDJPayRecordService;
import com.health.mapp.service.Adapter;
import com.health.mapp.service.SpringContextUtil;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.HttpClientHelper;

public class ThirdInterface {
	private static Logger logger = Logger.getLogger(ThirdInterface.class);
	/**
	 * user_phone  payment_id  amount payment_flag partner sign
	 * atime
	 * @param payCashId
	 * @param payOffTotal
	 * @param payment_flag
	 * @param UserId
	 */
	public void  payRecord(String payCashId,String payOffTotal,String payment_flag ,Long UserId){
		
		IUserLISTService userLISTService = SpringContextUtil.getBean("userLISTService");
		IThirdYHDJPayRecordService thirdYHDJPayRecordService=SpringContextUtil.getBean("thirdYHDJPayRecordService");
		UserLIST userInfo = userLISTService.selectByUserId(UserId);
		String user_phone = userInfo.getUserNo();
		String atime = String.valueOf(DateUtils.millisSecond());
		String sendFlag ="0";
		String receiveMsg="";
		try {
			String sign=this.geSign(atime, user_phone) ;
			String param = "user_phone="+user_phone+"&atime="+atime+"&sign="+sign+"&payment_id="+payCashId
							+"&amount="+payOffTotal+"&payment_flag="+payment_flag+"&partner="+PropertiesService.getString("partnerYHDJ").trim();
			
			receiveMsg=HttpClientHelper.sendGetHttp(PropertiesService.getString("YHDJ_URL").trim()+"payRecord.action", param);
			
			if (receiveMsg.contains("\"error_code\"")) {
				JSONObject jsStr = JSONObject.fromObject(receiveMsg);
				String error_coder=jsStr.getString("error_code");
				
				if (StringUtils.isNotBlank(error_coder)&&("0".equals(error_coder))) {
				}else{
					sendFlag="-1";
				}
			}else{
				sendFlag="-2";
			}
			ThirdYHDJPayRecord record= new ThirdYHDJPayRecord();
			record.setAmount(payOffTotal);
			record.setUserPhone(user_phone);
			record.setPaymentFlag(payment_flag);
			record.setPaymentId(payCashId);
			record.setSendFlag(sendFlag);
			record.setReceiveMsg(receiveMsg);
			thirdYHDJPayRecordService.insert(record);
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 短信提醒
	 * @param UserId
	 */
	public void docReply(Long UserId){
		
		IUserLISTService userLISTService = SpringContextUtil.getBean("userLISTService");
		UserLIST userInfo = userLISTService.selectByUserId(UserId);
		String user_phone = userInfo.getUserNo();
		String atime = String.valueOf(DateUtils.millisSecond());
		try {
			String sign=this.geSign(atime, user_phone) ;
			String param = "user_phone="+user_phone+"&atime="+atime+"&sign="+sign;
			String retuenStr =HttpClientHelper.sendGetHttp(PropertiesService.getString("YHDJ_URL").trim()+"docReply.action",	param);
			logger.debug(retuenStr);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
    private   String geSign( String atime, String user_phone) throws NoSuchAlgorithmException {

        String partner_key = PropertiesService.getString("partnerYHDJKey").trim();  
        String info = partner_key + atime + user_phone;
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] srcBytes = info.getBytes(); md5.update(srcBytes);
        byte[] resultBytes = md5.digest();
        String resultString = new String(new Hex().encode(resultBytes));
        return resultString.substring(8, 24);
    }
    

}
